package cn.zurish.cloud.mes.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.zurish.cloud.mes.dao.base.Material;
import cn.zurish.cloud.mes.dao.base.Parameter;
import cn.zurish.cloud.mes.dao.entity.BQ;
import cn.zurish.cloud.mes.dao.entity.BarCode;
import cn.zurish.cloud.mes.dao.mapper.BarCodeMapper;
import cn.zurish.cloud.mes.service.BarCodeService;
import cn.zurish.cloud.mes.toolikt.IDGenerator;
import com.alibaba.fastjson2.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static cn.zurish.cloud.mes.toolikt.FileReaderUtil.readBQFile;


@RestController
@RequiredArgsConstructor
@Slf4j
public class TestController {

    private final BarCodeMapper mapper;

    private final BarCodeService barCodeService;

    @GetMapping("/get")
    public BQ get() {
        BarCode barcode = mapper.selectById("BQ202504090351");
        log.info(barcode.getProductName());
        String jsonStr = barcode.getMaterials();

        List<Material> materials = JSON.parseArray(jsonStr, Material.class);
        materials.forEach(System.out::println);
        System.out.println(barcode.getMaterials());

        String jsonStr1 = barcode.getParameters();
        List<Parameter> parameters = JSON.parseArray(jsonStr1, Parameter.class);
        parameters.forEach(System.out::println);

        JSONArray jsonArray = JSONUtil.parseArray(barcode.getMaterials());
        String productName = barcode.getProductName();
        BQ bq = new BQ();
        bq.setExternalId(barcode.getExternalId());
        bq.setProductName(barcode.getProductName());
        bq.setMaterials(materials);
        bq.setParameters(parameters);

       // List<Material> materials = jsonArray.toList(Material.class);
       // materials.forEach(item -> System.out.println(item.getMaterialName()));
        return bq;
    }

    @GetMapping("/get1")
    public String insert() {
        // 准备固定的JSON内容
        String parametersJson = "[{\"techKey\":\"齿距\",\"techValue\":\"1.22-1.25mm\"},{\"techKey\":\"单宽\",\"techValue\":\"2.56-2.60mm\"},{\"techKey\":\"带宽\",\"techValue\":\"14-14mm\"},{\"techKey\":\"单厚\",\"techValue\":\"1.56-1.60mm\"}]";
        String materialsJson = "[{\"materialName\":\"150D/2普缝线(南通)\",\"materialBatch\":\"71502\",\"materialCode\":\"PZ2019121400048\",\"rawName\":\"下线\"},{\"materialName\":\"涤纶DTY150D/48F/重网Z/A13\",\"materialBatch\":\"SF61530/L\",\"materialCode\":\"PZ2019122500026\",\"rawName\":\"上线\"},{\"materialName\":\"3号尼龙新花纹布带\",\"materialBatch\":\"SB530010/LL\",\"materialCode\":\"PZ2024082100030\",\"rawName\":\"布带\"},{\"materialName\":\"N3卧式链条（过渡款）(本厂)\",\"materialBatch\":\"ds00026\",\"materialCode\":\"PZ2024043000136\",\"rawName\":\"链条\"}]";


        try {
            // 示例：读取当前目录下的BQ20250517.txt文件
            String[] contentArray = readBQFile("BQ.txt");
            List<BarCode> list = new ArrayList<>(512);

            // 创建并填充BarCode对象列表


            List<BarCode> barCodeList = new ArrayList<>(contentArray.length);
            for (String line : contentArray) {
                BarCode barCode = new BarCode();
                barCode.setExternalId(IDGenerator.generateId());
                barCode.setProductName(line);
                barCode.setParameters(parametersJson);
                barCode.setMaterials(materialsJson);
                barCodeList.add(barCode);
            }
            // 使用MyBatis-Plus批量插入
            barCodeService.saveBatch(barCodeList);
            // 打印数组内容
     /*       for (String line : contentArray) {

                System.out.println(line);
            }*/

        } catch (IOException e) {
            System.err.println("读取文件时出错: " + e.getMessage());
        } catch (IllegalArgumentException e) {
            System.err.println("文件格式错误: " + e.getMessage());
        }
       return "hello";
    }
}
// ssh-keygen -t ed25519 -C "zurish@qq.com"